//
//  AppDelegate.m
//  FAKER
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 YHLai. All rights reserved.
//

#import "AppDelegate.h"
#import "AllHeader.h"
#import "FitHeader.h"
#import "BaseClassImageView.h"

#define REDCOLOR [UIColor colorWithRed:0.98 green:0.28 blue:0.28 alpha:1]

@interface AppDelegate ()
@property(nonatomic, retain)PlayViewController *pv;
@property(nonatomic, retain)NSString *getVersion;
@property(nonatomic, strong)UITabBarController *tabBarController;
@property(nonatomic, strong)UINavigationController *nav1;
@property(nonatomic, strong)UINavigationController *nav2;
@property(nonatomic, strong)UINavigationController *nav3;
@property(nonatomic, strong)UINavigationController *nav4;
@property(nonatomic, strong)BaseClassImageView *launchImageView;
@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    _window = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
    _window.backgroundColor = [UIColor whiteColor];
    [_window makeKeyAndVisible];
    
    
    SubscriptionViewController *sv = [[SubscriptionViewController alloc]init];
    _nav1 = [[UINavigationController alloc]initWithRootViewController:sv];
    _nav1.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"订阅" image:[UIImage imageNamed:@"icon_newboxview_boxview_tab@3x"] tag:1000];
    
    //修改热点为我的
    HotViewController *hv = [[HotViewController alloc]init];
    _nav2 = [[UINavigationController alloc]initWithRootViewController:hv];
    _nav2.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"我的" image:[UIImage imageNamed:@"icon_newboxview_messagecenter_tab@3x"] tag:1001];

    CommunityViewController *cv = [[CommunityViewController alloc]init];
    _nav4 = [[UINavigationController alloc]initWithRootViewController:cv];
    _nav4.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"社区" image:[UIImage imageNamed:@"icon_topic_tab@3x"] tag:1003];
    
    _pv = [[PlayViewController alloc]init];
    [self getData];
    _nav3 = [[UINavigationController alloc]initWithRootViewController:_pv];
    _nav3.tabBarItem = [[UITabBarItem alloc]initWithTitle:@"玩乐" image:[UIImage imageNamed:@"icon_newboxview_life_tab@3x"] tag:1004];
    
    _tabBarController = [[UITabBarController alloc]init];
    [[UINavigationBar appearance] setBarTintColor:[UIColor colorWithRed:0.98 green:0.28 blue:0.28 alpha:1]];
    _tabBarController.viewControllers = @[_nav1, _nav3, _nav4, _nav2];//修改排序
    _tabBarController.tabBar.translucent = 0;
//    _tabBarController.tabBar.tintColor = [UIColor colorWithRed:0.98 green:0.28 blue:0.28 alpha:1];
    [self changeColor];
    _window.rootViewController = _tabBarController;

    //修改navigationBar上的所有控件为白色
    [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
    
    //修改时间栏字体颜色为白色
    [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleLightContent animated:YES];
    
    //添加夜间模式监听
    ADD_NSNOTIFICATIONCENTER_OBSERVER;
#pragma mark 消息中心

    
    
    
    
#pragma mark 3D Touch(让人讨厌的注释)
    //图标初始化
    UIApplicationShortcutIcon *channelIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@"Channel"];
    UIApplicationShortcutIcon *aboutUsIcon = [UIApplicationShortcutIcon iconWithTemplateImageName:@""];
    
    //创建shortcut items(要可变的啊)
    UIMutableApplicationShortcutItem *channelItem = [[UIMutableApplicationShortcutItem alloc]initWithType:@"channel" localizedTitle:@"频道" localizedSubtitle:@"快速进入频道和搜索页面" icon:channelIcon userInfo:nil];
    UIMutableApplicationShortcutItem *aboutUsItem = [[UIMutableApplicationShortcutItem alloc]initWithType:@"aboutUs" localizedTitle:@"关于我们" localizedSubtitle:@"软件开发者页面" icon:aboutUsIcon userInfo:nil];
    
    //字面量形势将items放进数组中
    NSArray *itemsArray = @[channelItem, aboutUsItem];
    
    //把items数组给单例
    [UIApplication sharedApplication].shortcutItems = itemsArray;
    
    //启动页
    _launchImageView = [[BaseClassImageView alloc] initWithFrame:_window.frame];
    _launchImageView.image = [UIImage imageNamed:@"1"];
    [_window addSubview:_launchImageView];
    //放大动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    basicAnimation.fromValue = [NSNumber numberWithDouble:1];//原始比例
    basicAnimation.toValue = [NSNumber numberWithDouble:1.3];//放大倍数
    [basicAnimation setDuration:3];
    [_launchImageView.layer addAnimation:basicAnimation forKey:@"launch"];
    
    //移除启动页
    [UIView animateWithDuration:3 animations:^{
        _launchImageView.alpha = 0;
    }];
    [UIView animateWithDuration:3 animations:^{
        _launchImageView.alpha = 0;
    } completion:^(BOOL finished) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [_launchImageView removeFromSuperview];
        });
    }];
    
    return YES;
}

- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler
{
        [NSNOTIFICATION_DEFAULTCENTER addObserver:self selector:@selector(pushSearchViewController) name:@"3D Touch" object:nil];
    if ([shortcutItem.type isEqualToString:@"channel"]) {
//        WFChannelViewController *wc = [[WFChannelViewController alloc]init];
//        [_window.rootViewController presentViewController:wc animated:1 completion:nil];
        [NSNOTIFICATION_DEFAULTCENTER postNotificationName:@"3D Touch" object:nil];
    }
}

#pragma mark 獲取版本信息
-(void)getData
{
    NSString *urlString = @"http://itunes.apple.com/CN/lookup?id=462149227";
    [InternetDataRequest getDataByString:urlString BodyDic:nil WithDataBlock:^(id data) {
        NSArray *array = [data objectForKey:@"results"];
        NSDictionary *dic = [array objectAtIndex:0];
        _getVersion = [dic objectForKey:@"version"];
        _pv.version = _getVersion;
    }];
}
#pragma mark 改变颜色
- (void)changeColor
{
    UIView *grayLineView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, .5)];
    [_tabBarController.tabBar addSubview:grayLineView];
    if (NSUSERDEFAULTS_NIGHT) {
        _nav1.navigationBar.barTintColor = BGCOLOR_BLACK;
        _nav2.navigationBar.barTintColor = BGCOLOR_BLACK;
        _nav3.navigationBar.barTintColor = BGCOLOR_BLACK;
        _nav4.navigationBar.barTintColor = BGCOLOR_BLACK;
        _tabBarController.tabBar.barTintColor = BGCOLOR_BLACK;
        _tabBarController.tabBar.tintColor = [UIColor colorWithRed:0.89 green:0.89 blue:0.9 alpha:1];
        [[UINavigationBar appearance] setTintColor:[UIColor lightGrayColor]];
        grayLineView.backgroundColor = [UIColor darkGrayColor];
    }
    else {
        _nav1.navigationBar.barTintColor = REDCOLOR;
        _nav2.navigationBar.barTintColor = REDCOLOR;
        _nav3.navigationBar.barTintColor = REDCOLOR;
        _nav4.navigationBar.barTintColor = REDCOLOR;
        _tabBarController.tabBar.barTintColor = BGCOLOR_WHITE;
        _tabBarController.tabBar.tintColor = [UIColor colorWithRed:0.98 green:0.28 blue:0.28 alpha:1];
        [[UINavigationBar appearance] setTintColor:[UIColor whiteColor]];
        grayLineView.backgroundColor = [UIColor whiteColor];
    }
}


- (void)pushSearchViewController
{
    WFChannelViewController *wc = [[WFChannelViewController alloc]init];
    [_nav1  pushViewController:wc animated:1];
}



- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
